package com.edushare.common.mailer;

import com.edushare.common.util.URLUtils;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.OutgoingEmails;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.UserRoles;
import com.edushare.common.vo.Users;

public class Invite extends MailStringLoader {
	public Invite(Users to, CourseInstances course, int roleType, Users from){
		String keyText = "";
		String keyURL = "";
		rec = to;
		String roleName = "";
		if(to.getStatus() == Users.INVITED){
			keyText = "Accept";
			keyURL  = URLUtils.generateTeacherInviteURL(to.getConfKey());
		}
		else{
			keyText = "View";
			keyURL = URLUtils.getHomePageURL(course.getCourse());
		}		
		
		switch(roleType){
		case(Enrolments.TEACHER):roleName = "Teacher";break;
		case(Enrolments.TEACHING_ASSISTANT):roleName = "Teaching Assistant";break;
		case(Enrolments.STUDENT):roleName = "Student";break;
		case(Enrolments.OBSERVER):roleName = "Auditor(You will not be awarded grades)";break;
		}
		init(from, course.getCourse().getCourseName(), URLUtils.getHomePageURL(course.getCourse()), 
				roleName, keyURL,keyText);
	}
	
	public Invite(Users u, Departments dept, int roleType, Users from){
		rec = u;
		String keyText = "";
		String keyURL = "";
		String roleName = "";
		if(u.getStatus() == Users.INVITED){
			keyText = "Accept";
			keyURL  = URLUtils.generateTeacherInviteURL(u.getConfKey());
		}
		else{
			keyText = "View";
			keyURL = URLUtils.getHomePageURL(dept);
		}		
		
		switch(roleType){
		case(UserDeptLink.PRIMARY_ADMIN):roleName = "Administrator";break;
		case(UserDeptLink.SECONDARY_ADMIN):roleName = "Administrator";break;
		case(UserDeptLink.TEACHER):roleName = "Teacher";break;
		case(UserDeptLink.STUDENT):roleName = "Student";break;
		}
		init(from, dept.getDeptName(), URLUtils.getHomePageURL(dept), roleName, keyURL,keyText);		
	}
	
	public Invite(Users u, Universities univ, int roleType, Users from){
		rec = u;
		String keyText = "";
		String keyURL = "";
		String roleName = "";
		if(u.getStatus() == Users.INVITED){
			keyText = "Accept";
			keyURL  = URLUtils.generateTeacherInviteURL(u.getConfKey());
		}
		else{
			keyText = "View";
			keyURL = URLUtils.getHomePageURL(univ);
		}		
		
		switch(roleType){
		case(UserRoles.ADMIN_ROLE):roleName = "Administrator";break;
		case(UserRoles.TEACHER_ROLE):roleName = "Teacher";break;
		case(UserRoles.STUDENT_ROLE):roleName = "Student";break;
		}
		init(from, univ.getUniversityName(), URLUtils.getHomePageURL(univ), roleName, keyURL,keyText);
	}
	
	private void init(Users from, String entityName, String entityURL, String roleName,
			String keyURL, String keyText){
		replaceMap.put("FROM_USER_NAME", from.getCompleteUserName());
		replaceMap.put("FROM_USER_URL", URLUtils.getHomePageURL(from));
		replaceMap.put("ENTITY_URL", entityURL);
		replaceMap.put("ENTITY_NAME", entityName);
		replaceMap.put("KEY_URL", keyURL);
		replaceMap.put("HOMEPAGE_TEXT", keyText);
		replaceMap.put("CUSTOM_MESSAGE", "");
		replaceMap.put("ROLENAME", roleName);
		
		messageString = templateParser(mailString, replaceMap);
	}
	
	private Users rec = null;
	private String mailString = MailBase.get().stringMap.get(MailBase.INVITE_MAIL);
	
	@Override
	public OutgoingEmails send() {
		OutgoingEmails mail = new OutgoingEmails();
		mail.setContent(messageString);
		mail.setReceiverEmailId(rec.getEmail());
		mail.setStatus(OutgoingEmails.EMAIL_TO_BE_SENT);
		mail.setCopyType(1);
		mail.setSubject("EduShare Invitation");
		return mail;				
	}
}
